// tslint:disable:jsx-no-multiline-js
// tslint:disable:jsx-no-lambda
import * as React from 'react';
import * as H from 'history';
import {
    RouteComponentProps,
    Route,
    Redirect
} from 'react-router-dom';
import {
    DEFAULT_REDIRECT_PATH,
    defaultAuthVerify
} from './index';

export const AuthRoute = (
    { component: Component, ...rest }:
        {
            location?: H.Location,
            component: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>,
            render?: ((props: RouteComponentProps<any>) => React.ReactNode),
            children?: ((props: RouteComponentProps<any>) => React.ReactNode) | React.ReactNode,
            path?: string,
            exact?: boolean,
            strict?: boolean
        }) => (
        <Route
            {...rest}
            render={(props: any) => (
                defaultAuthVerify(props)
                    ? (
                        <Component {...props} />
                    ) : (
                        <Redirect
                            to={{
                                pathname: DEFAULT_REDIRECT_PATH,
                                state: { from: props.location }
                            }}
                        />
                    )
            )}
        />
    );
